package my.xduscorequery;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;

import org.apache.http.client.ClientProtocolException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.webkit.WebView;

public class ShowScore extends Activity {
	private String
		uid=null,
		psw=null,
		data=null;
	private boolean useProxy;
	private WebView
		webview = null;
	
	private Handler
		handler = new Handler(){
		public void handleMessage(Message msg){
			if(webview!=null){
				//					webview.loadData(URLEncoder.encode(data, "utf8"), "text/html", "utf8");
				webview.loadDataWithBaseURL("", data, "text/html", "utf-8","");
			}
		}
	};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_score);
        Intent intent=getIntent();
        if(intent!=null){
        	uid=intent.getStringExtra("UID");
        	psw=intent.getStringExtra("PSW");
        	useProxy=intent.getBooleanExtra("PXY",false);
        }
        webview=(WebView)findViewById(R.id.webView1);
        new FetchThread().start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_show_score, menu);
        return true;
    }
    
    class FetchThread extends Thread{
    	public void run(){
			try {
				data=ScoreFetcher.fetchScore(uid,psw,useProxy);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		if(data!=null)
    			handler.sendEmptyMessage(0);
    	}
    }
    
}
